﻿using Microsoft.AspNetCore.Authorization;
using System.Collections.Generic;
using CRM.Extensions;
using System.Threading.Tasks;

namespace CRM.Authorization.Rule
{
    public class Handler : HandlerAbs<Requirement, PermissionAttribute>
    {
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
            Requirement requirement,
            IEnumerable<PermissionAttribute> attributes)
        {
            foreach (var item in attributes)
            {
                if (!context.User.HasPermission(item.Type, item.Enum))
                {
                    context.Fail();
                    return Task.CompletedTask;
                }
            }

            context.Succeed(requirement);
            return Task.CompletedTask;
        }
    }
}